package com.onlineshop.highgee.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.onlineshop.highgee.common.BasicDao;
import com.onlineshop.highgee.common.JdbcUtil;
import com.onlineshop.highgee.dao.UserInfoDao;
import com.onlineshop.highgee.entity.UserInfo;

public class UserDaoInfoImpl implements UserInfoDao {

	@Override
	public void addUserInfo(UserInfo userInfo) {
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			// // 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
			// Class.forName("com.mysql.jdbc.Driver");
			// // 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
			// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
			// "root", "root");
			// // String sql = "insert into highgee_user values(?,?)";
			String sql = "insert into user_info values(?,?,?,?,?,?)";
			conn = JdbcUtil.getConnection();
			BasicDao.updateData(sql, conn, userInfo.getUserInfoId(), userInfo.getAddress(), userInfo.getEmail(),
					userInfo.getPhone(), userInfo.getSex(), userInfo.isUserState());

			// pst = conn.prepareStatement(sql);
			// pst.setInt(1, userInfo.getUserInfoId());
			// pst.setString(2, userInfo.getAddress());
			// pst.setString(3, userInfo.getEmail());
			// pst.setString(4, userInfo.getPhone());
			// pst.setString(5, userInfo.getSex());
			// pst.setBoolean(6, userInfo.isUserState());
			// pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// finally {
		// try {
		// if (pst != null)
		// pst.close();
		// if (conn != null)
		// conn.close();
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		// }
	}

	@Override
	public void updateUserInfo(UserInfo userInfo) {
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			// 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
			// Class.forName("com.mysql.jdbc.Driver");
			// 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
			// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
			// "root", "root");
			String sql = "update user_info set address=?,email=?,phone=?,sex=?,user_state=? where userinfo_id=?";
			conn = JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			BasicDao.updateData(sql, conn, userInfo.getAddress(), userInfo.getEmail(), userInfo.getPhone(),
					userInfo.getSex(), userInfo.isUserState(), userInfo.getUserInfoId());
			conn.commit();

			// pst = conn.prepareStatement(sql);
			// pst.setInt(6, userInfo.getUserInfoId());
			// pst.setString(1, userInfo.getAddress());
			// pst.setString(2, userInfo.getEmail());
			// pst.setString(3, userInfo.getPhone());
			// pst.setString(4, userInfo.getSex());
			// pst.setBoolean(5, userInfo.isUserState());
			// pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null)
					pst.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void deleteUserInfo(UserInfo userInfo) {
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			// 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
			// Class.forName("com.mysql.jdbc.Driver");
			// 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
			// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
			// "root", "root");
			String sql = "delete from user_info where userinfo_id=?";
			conn = JdbcUtil.getConnection();
			pst = conn.prepareStatement(sql);
			pst.setInt(1, userInfo.getUserInfoId());
			pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null)
					pst.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<UserInfo> findAll() {
		List<UserInfo> userInfoList = new ArrayList<UserInfo>();
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		UserInfo userInfo = null;
		try {
			// 锟斤拷锟斤拷锟斤拷锟斤拷 oracle.jdbc.driver.OracleDriver
			// Class.forName("com.mysql.jdbc.Driver");
			// 锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷锟绞讹拷锟斤拷 执锟斤拷 锟斤拷锟斤拷 锟酵凤拷锟斤拷源
			// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlineshop",
			// "root", "root");
			conn = JdbcUtil.getConnection();
			String sql = "select * from user_info";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				userInfo = new UserInfo();
				userInfo.setUserInfoId(rs.getInt("userinfo_id"));
				userInfo.setAddress(rs.getString("address"));
				userInfo.setEmail(rs.getString("email"));
				userInfo.setPhone(rs.getString("phone"));
				userInfo.setSex(rs.getString("sex"));
				userInfo.setUserState(rs.getBoolean("user_state"));
				userInfoList.add(userInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null)
					pst.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return userInfoList;
	}

}
